package com.toommi.refuse.controller;

import com.toommi.refuse.annotation.PermissionController;
import com.toommi.refuse.annotation.PermissionReport;
import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.entity.Motorcade;
import com.toommi.refuse.exception.AdminException;
import com.toommi.refuse.service.MotorcadeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * ..
 * 车队操作
 *
 * @author yangg
 * @since 2019-07-31 16:45
 */
@RestController
@RequestMapping(value = "")
@Slf4j
@AllArgsConstructor
@PermissionController(groupCode = "admin_motorcade", groupName = "车队管理")
@Api(tags = "车队信息")
public class MotorcadeController {

    private MotorcadeService motorcadeService;

    /**
     * 新增车队信息
     *
     * @param motorcade 车队信息
     * @return 操作结果
     */
    @ApiOperation(value = "新增车队信息", notes = "新增车队信息")
    @ApiImplicitParam(name = "motorcade", value = "车队信息", required = true, dataType = "Motorcade")
    @RequiresPermissions("admin:motorcade:add")
    @PermissionReport(value = "admin:motorcade:add", name = "新增车队", group = "admin_motorcade")
    @PostMapping(value = "/admin/motorcade", produces = "application/json;charset=UTF-8")
    public CommonResponse addMotorcade(@RequestBody Motorcade motorcade) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            motorcadeService.addMotorcade(motorcade);
        } catch (AdminException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add motorcade error:{}", e);
        }
        return commonResponse;
    }

    /**
     * 删除指定车队信息
     *
     * @param id 车队信息id
     * @return 删除结果
     */
    @ApiOperation(value = "删除车队信息", notes = "删除车队信息")
    @ApiImplicitParam(name = "id", value = "车队信息id", required = true, dataType = "Long")
    @RequiresPermissions("admin:motorcade:delete")
    @PermissionReport(value = "admin:motorcade:delete", name = "删除车队", group = "admin_motorcade")
    @DeleteMapping(value = "/admin/motorcade", produces = "application/json;charset=UTF-8")
    public CommonResponse deleteMotorcade(@PathVariable(name = "id") long id) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            motorcadeService.deleteMotorcade(id);
        } catch (Exception e) {
            log.error("delete motorcade error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 查询车队列表信息
     *
     * @param pageNum  页码
     * @param pageSize 每页数据量
     * @param keyWord  查询关键字
     * @return 车队列表信息
     */
    @ApiOperation(value = "查询车队信息", notes = "查询车队信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "keyWord", value = "查询关键字", required = false, dataType = "String")
    })
    @GetMapping(value = "/api/motorcades", produces = "application/json;charset=UTF-8")
    public CommonResponse findMotorcades(@RequestParam int pageNum, @RequestParam int pageSize,
                                         @RequestParam String keyWord) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(motorcadeService.findMotorcades(pageNum, pageSize, keyWord));
        } catch (Exception e) {
            log.error("find motorcade error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }
}
